#!/bin/sh
SCRIPT_NAME=${0##*/}
ELOSD_LOG_FILE="/tmp/elosd.log"
ELOSD_PID_FILE="/run/elosd.pid"

check_for_elos() {
    ELOS_PID="-1"
    if [ -e "${ELOSD_PID_FILE}" ]
    then
        ELOS_PID=$(cat "${ELOSD_PID_FILE}")
    fi

    if [ -d "/proc/${ELOS_PID}" ]
    then
        return 0
    else
        return 1
    fi
}

start() {
    if check_for_elos
    then
        echo "elos already running"
        exit 1
    fi

    touch "${ELOSD_LOG_FILE}"
    chmod 666 "${ELOSD_LOG_FILE}"

    @CMAKE_INSTALL_FULL_BINDIR@/elosd > "${ELOSD_LOG_FILE}" 2>&1 &
    ELOS_PID=$!

    sleep 0.2s

    if [ -d "/proc/$ELOS_PID" ]
    then
        if [ -e "${ELOSD_PID_FILE}" ]
        then
            rm -f "${ELOSD_PID_FILE}"
        fi
        echo $ELOS_PID > "${ELOSD_PID_FILE}"
    fi
}

stop() {
    if check_for_elos
    then
        ELOSD_PID=$(cat "${ELOSD_PID_FILE}")
        kill "${ELOSD_PID}" >> "${ELOSD_LOG_FILE}" 2>&1
        NUMBER_RETRIES=10
        while [ -d "/proc/${ELOSD_PID}" ]
        do
            sleep 0.2
            if [ ${NUMBER_RETRIES} -gt 0 ]
            then
                NUMBER_RETRIES=$((NUMBER_RETRIES-1))
            else
                echo "timeout to stop elosd, kill it now" | tee "${ELOSD_LOG_FILE}" 2>&1
                echo "kill ${ELOSD_PID}"
                kill -9 ${ELOSD_PID} >> "${ELOSD_LOG_FILE}" 2>&1
                break;
            fi
        done
        rm -f "${ELOSD_PID_FILE}"
    else
        echo "elos already stopped"
        exit 1
    fi
}

status() {
    if check_for_elos
    then
        echo "Status : Running"
    else
       echo "Status : Stopped"
    fi
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status
        ;;
  restart)
        stop
        start
        ;;
  *)
        echo "Usage: ${SCRIPT_NAME} {start|stop|restart|status}"
        exit 1
esac

exit 0
